package com.tianqu.tms.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.tianqu.tms.core.exception.TmsDaoException;
import com.tianqu.tms.dto.MarketCarHandleDto;
import com.tianqu.tms.entity.MarketCarHandleInfo;


public class MarketCarHandleInfoDaoImp  extends ViewListDao implements MarketCarHandleInfoDao  {
	protected MarketCarHandleInfoDaoImp() throws TmsDaoException {
		super();
	}
	public List<MarketCarHandleDto> findHandleInfoBySerial(String serial) throws TmsDaoException {
		List<MarketCarHandleDto> r = new ArrayList<MarketCarHandleDto>();
		PreparedStatement ps = null;
		ResultSet rs = null;

		final StringBuffer sql = new StringBuffer("");
		sql.append(" SELECT ");
		sql.append(" MARKET.ID AS ID, ");
		sql.append(" MARKET.SERIAL AS SERIAL , ");
		sql.append(" MARKET.HANDLETYPE AS HANDLETYPE , ");
		sql.append(" MARKET.ISPUSH AS ISPUSH, ");
		sql.append(" MARKET.PUSHCONTENT AS PUSHCONTENT,");
		sql.append(" MARKET.PHOTO AS PHOTO,");
		sql.append(" MARKET.REMARK AS REMARK,");
		sql.append(" MARKET.UPDATEUSERID AS UPDATEUSERID, ");
		sql.append(" MARKET.UPDATETIME AS UPDATETIME, ");
		sql.append(" USER.REALNAME AS REALNAME ");
		sql.append(" FROM ");
		sql.append(TmsTableNames.TMS_MARKET_CAR_HANDLEINFO);
		sql.append(" MARKET ");
		sql.append(" LEFT JOIN  ");
		sql.append(TmsTableNames.TMS_USER);
		sql.append(" USER ");
		sql.append(" ON MARKET.UPDATEUSERID=USER.ID");
		sql.append(" WHERE ");
		sql.append(" SERIAL=? ");
		sql.append(" ORDER BY UPDATETIME ASC");
		try {
			ps = conn.prepareStatement(sql.toString());
			ps.setString(1, serial);
			rs = ps.executeQuery();
			while (rs.next()) {
				MarketCarHandleDto dto = new MarketCarHandleDto();
				dto.setId(rs.getLong("ID"));
				dto.setSerial(rs.getString("SERIAL"));
				dto.setHandleType(rs.getInt("HANDLETYPE"));
				dto.setIsPush(rs.getInt("ISPUSH"));
				dto.setPushContent(rs.getString("PUSHCONTENT"));
				dto.setPhoto(rs.getString("PHOTO"));
				dto.setUpdateUserId(rs.getInt("UPDATEUSERID"));
				dto.setUpdateTime(rs.getTimestamp("UPDATETIME"));
				dto.setUserName(rs.getString("REALNAME"));
				dto.setRemark(rs.getString("REMARK"));
				r.add(dto);
			}
			return r;
		} catch (SQLException e) {
			throw new TmsDaoException(e.toString());

		} finally {
			super.close(rs, ps);
		}		
	}
	public void insertCarHandleInfo(MarketCarHandleDto dto)throws TmsDaoException{
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			StringBuffer sql = new StringBuffer("INSERT INTO ");
			sql.append(TmsTableNames.TMS_MARKET_CAR_HANDLEINFO);
			sql.append(" (");		
			sql.append(" SERIAL, ");
			sql.append(" HANDLETYPE, ");
			sql.append(" ISPUSH, ");
			sql.append(" PUSHCONTENT, ");
			sql.append(" PHOTO, ");
			sql.append(" REMARK, ");
			sql.append(" UPDATEUSERID, ");		
			sql.append(" UPDATETIME ");
			sql.append(" ) VALUES ( ");
			sql.append(" ?,?,?,?,?,?,?,SYSDATE() )");

			ps = conn.prepareStatement(sql.toString());
			
			ps.setString(1, dto.getSerial());
			ps.setInt(2, dto.getHandleType());
			ps.setInt(3, dto.getIsPush());
			ps.setString(4, dto.getPushContent());
			ps.setString(5, dto.getPhoto());
			ps.setString(6, dto.getRemark());
			ps.setInt(7, dto.getUpdateUserId());
			
			ps.executeUpdate();
			
		} catch (SQLException e) {
			throw new TmsDaoException(e.toString());

		} finally {
			super.close(rs, ps);
		}
	}

    @Override
    public void updateCarHandleInfo(MarketCarHandleInfo carHandleInfo) throws TmsDaoException{
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			StringBuffer sql = new StringBuffer("UPDATE ");
			sql.append(TmsTableNames.TMS_MARKET_CAR_HANDLEINFO);
			sql.append(" SET ");
			sql.append(" HANDLETYPE=?,");
			sql.append(" ISPUSH=?,");
			sql.append(" PUSHCONTENT=?,");
			sql.append(" UPDATEUSERID=?,");
			sql.append(" UPDATETIME=now()");
			sql.append(" WHERE ");
			sql.append(" SERIAL=?");
			ps = conn.prepareStatement(sql.toString());
			ps.setInt(1, carHandleInfo.getHandleType());
			ps.setInt(2, carHandleInfo.getIsPush());
			ps.setString(3, carHandleInfo.getPushContent());
			ps.setInt(4, carHandleInfo.getUpdateUserId());
			ps.setString(5, carHandleInfo.getSerial());

			ps.executeUpdate();

		} catch (SQLException e) {
			throw new TmsDaoException(e.toString());

		} finally {
			super.close(rs, ps);
		}
    }
}
